package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;

import java.rmi.ServerException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<GeofenceVO> selectAll(GeofenceQuery geofenceQuery) {
        log.debug("开始处理【查询电子围栏】的业务，参数：{}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectAll(geofenceQuery);
        log.debug("查询结果：{}", list.size());
        for (int i = 0; i < list.size(); i++) {
            //addGeofenceList(geofenceVO); //自己写的
            // 2.依次取出每一个围栏对象
            GeofenceVO geofenceVO = list.get(i);
            // 3.调用车辆mapper层的查询方法,查看本围栏下的所有已经绑定的车辆
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectAll(vehicleQuery);
            // 4.获取当前围栏绑定车辆总数
            int totalNum = vehicleVOList.size();
            // 5.定义变量保存可用车辆数
            int availableNum = 0;
            // 6.依次遍历每辆车,如果车辆的状态 1(空闲) ,则可用车辆数+1
            for(VehicleVO vehicleVO : vehicleVOList){
                if ("1".equals(vehicleVO.getStatus())) {
                    availableNum++;
                }
            }
            // 7.将准备好的数据赋值到当前围栏对象中
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setAvailableNum(availableNum);
            // 注意:此处的车辆列表详情会在后续分配车辆查看车辆详情时使用,所以此处要一起返回!
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }
    //自己写的方法,用于查询 车辆
    private void addGeofenceList(GeofenceVO geofenceVO) {
        // 准备空集合,用来保存 围栏可用车辆数 和 围栏车辆总数 和 围栏绑定车辆列表
        List<Integer> availableNumList = new ArrayList<>();
        // 根据电子围栏id 查出电子围栏绑定车辆列表
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(geofenceVO.getId());
        List<VehicleVO> vehicleVOList = vehicleMapper.selectAll(vehicleQuery);
        for (int i = 0; i < vehicleVOList.size(); i++) {
            VehicleVO vehicleVO = vehicleVOList.get(i);
            if ("1".equals(vehicleVO.getStatus())) {
                availableNumList.add(1);
            }
        }
        geofenceVO.setTotalNum(vehicleVOList.size());
        geofenceVO.setAvailableNum(availableNumList.size());
        geofenceVO.setVehicleList(vehicleVOList);
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("开始处理【保存电子围栏】的业务，参数：{}", geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam, geofence);
        //判断是否有id,如果没有添加，如果有修改
        if (geofence.getId() == null) {
            geofence.setStatus("1");
            geofence.setCreateTime(new Date());
            geofenceMapper.insertGeofence(geofence);
        } else {
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }

    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("开始处理【删除电子围栏】的业务，参数：{}", id);
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list = vehicleMapper.selectAll(vehicleQuery);
        //判断围栏有没有绑定车辆,如果有绑定车辆，则不能删除,否则可以删除
        if( list.size()>0 && list != null){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        //删除
        geofenceMapper.deleteGeofence(id);
    }
}
